package com.bjpowernode.crm.settings.web.controller;

import cn.hutool.core.util.ObjectUtil;
import com.bjpowernode.crm.commons.enums.RespCode;
import com.bjpowernode.crm.commons.resp.Result;
import com.bjpowernode.crm.settings.domain.DicType;
import com.bjpowernode.crm.settings.service.DicTypeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.jws.WebService;
import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * ClassName:DicTypeController
 * Package:com.bjpowernode.crm.settings.web.controller
 * Date:2022/6/3 16:17
 * Description:
 * Author:zsm@zhangshuming.com
 */
@Controller
public class DicTypeController {


    @RequestMapping("/settings/dictionary/type/index.do")
    public String index(){
        return "settings/dictionary/type/index";
    }
    // 业务逻辑层接口
    @Autowired
    private DicTypeService dicTypeService;

    @RequestMapping("/settings/dictionary/type/queryAllDicType.do")
    @ResponseBody
    public Object queryAllDicType(){
        // 创建应答（响应）结果对象
        Result result = Result.FRR();
        try {

            List<DicType> dicTypes = dicTypeService.queryAllDicType();

            result.setRespCode(RespCode.SUCCESS);

            result.setBody(dicTypes);

        } catch (Exception e) {

            e.printStackTrace();
            return result;
        }
        return result;
    }

    @RequestMapping("/settings/dictionary/type/createDicTypePage.do")
    public String createDicTypePage(){
        return "settings/dictionary/type/save";
    }

    @RequestMapping("/settings/dictionary/type/checkCode.do")
    public @ResponseBody Result checkCode(String code){
        Result result = Result.FRR();
        try {
            DicType dicType = dicTypeService.checkCode(code);
            if (ObjectUtil.isNull(dicType)){
                result = Result.SUCCESS();
            }else {
                result.setRespCode(RespCode.TYPE_CODE_EXISTS);
            }
        } catch (Exception e) {
            e.printStackTrace();
            return result;
        }
        return result;
    }

    @RequestMapping("/settings/dictionary/type/saveCreateDicTyped.do")
    public @ResponseBody Result saveCreateDicTyped(DicType dicType){
        Result result = Result.FRR();
        try {
           int dicTyped = dicTypeService.saveCreateDicTyped(dicType);
           if (dicTyped == 1){
               result.setRespCode(RespCode.SUCCESS);
           }
        } catch (Exception e) {
            e.printStackTrace();
            return result;
        }
        return result;
    }


    @RequestMapping("/settings/dictionary/type/updateDicTypePage.do")
    public String updateDicTypePage(HttpServletRequest request, String code){
        DicType dicTypeCode = dicTypeService.queryDicTypeByCode(code);
        request.setAttribute("dicType",dicTypeCode);
        return "settings/dictionary/type/edit";
    }

    @RequestMapping("settings/dictionary/type/updateEditDicTyped.do")
    public @ResponseBody Result updateEditDicTyped(DicType dicType){
        Result result = Result.FRR();
        int dicTyped = dicTypeService.updateEditDicTyped(dicType);
        if (dicTyped == 1){
            result.setRespCode(RespCode.SUCCESS);
        }
        try {
        } catch (Exception e) {
            e.printStackTrace();
            result.setRespCode(RespCode.FAILURE);
        }
        return result;
    }

    @RequestMapping("settings/dictionary/type/deleteDicType.do")
    public @ResponseBody Result deleteDicType(String[] code){
        Result result = Result.FRR();
        try {
            int results = dicTypeService.deleteDicType(code);
            if (ObjectUtil.isNotNull(results)){
                result.setRespCode(RespCode.SUCCESS);
            }
        } catch (Exception e) {
            e.printStackTrace();
            return result;
        }
        return result;
    }
}
